package szf.im.server.data;

import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author shizhanfu
 */
public class OnlineHelp {
    private static ConcurrentHashMap<String, Channel> Connections = new ConcurrentHashMap<String, Channel>();

    public static void add(String id, Channel channel) {
        Channel cc = Connections.get(id);
        if (cc == null) {
            Connections.put(id, channel);
        }
    }

    public static void remove(String id) {
        if (Connections.get(id) != null) {
            Connections.remove(id);
        }
    }

    public static int size() {
        int size = Connections.size();
        System.out.println("当前在线人数:" + size);
        return size;
    }

    /**
     * 广播消息
     *
     * @param currentId
     */
    public static void publishMsg(int currentId, String msg) {
        Connections.forEach((k, v) -> {
            v.writeAndFlush(ResultWrapper.simpleMsg(currentId, msg));
//            if (k != currentId) {
//                v.writeAndFlush(ResultWrapper.simpleMsg(msg));
//            }
        });
    }
}
